/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package datamodels;

import database_beans.Zaposleni;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.primefaces.model.SelectableDataModel;
import server.Util;

/** Used for Lazy loading data from database 
 *
 * @author stefan
 */
public class ZaposleniDataModel extends ListDataModel<Zaposleni> implements SelectableDataModel<Zaposleni>{
    
    private ArrayList<Zaposleni> datasource;

    public ZaposleniDataModel(ArrayList<Zaposleni> datasource) {
        super(datasource);
        this.datasource = datasource;
        load();
    }
    
    public void load(){
        Session session = Util.getSesion();
        Criteria userCriteria = session.createCriteria(Zaposleni.class);
        datasource = (ArrayList<Zaposleni>)userCriteria.list();
        
        
        
//        return datasource;
    }
    
    @Override
    public int getRowCount(){
        return datasource.size();
    }

    @Override
    public Object getRowKey(Zaposleni zap) {
        return zap.getMail();
    }
    
    
    @Override
    public Zaposleni getRowData(String rowKey){
        
        List<Zaposleni> list = ( List<Zaposleni>)getWrappedData();
        for(Zaposleni zap:datasource){
            if(zap.getMail().equals(rowKey)){
                return zap;
            }
        }
        return null;
    }
    
    
}
